import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D
from paddle.fluid.initializer import NumpyArrayInitializer

img = Image.open('./work/images/section1/000000098520.jpg')
with fluid.dygraph.guard():
    # 设置卷积核参数
    w = np.array([[-1,-1,-1], [-1,8,-1], [-1,-1,-1]], dtype='float32')/8
    w = w.reshape([1, 1, 3, 3])
    """
    [
      [
        [
          [-0.125 -0.125 -0.125]
          [-0.125  1.    -0.125]
          [-0.125 -0.125 -0.125]
        ]
      ]
    ]
    """
    # 由于输入通道数是3，将卷积核的形状从[1,1,3,3]调整为[1,3,3,3]
    #@ axis是层数的意思，将 【】下第一个层级为0第二层级为1，axis指向的层级给予赋值3次，复制的内容是w
    w = np.repeat(w, 3, axis=1)
    """w的使用的数据内容是： 
    [
      [
        [
          [-0.125 -0.125 -0.125]
          [-0.125  1.    -0.125]
          [-0.125 -0.125 -0.125]
        ],
        [
          [-0.125 -0.125 -0.125]
          [-0.125  1.    -0.125]
          [-0.125 -0.125 -0.125]
        ],
        [
          [-0.125 -0.125 -0.125]
          [-0.125  1.    -0.125]
          [-0.125 -0.125 -0.125]
        ]
      ]
    ]

    """
    # 创建卷积算子，输出通道数为1，卷积核大小为3x3，
    # 和案例1是一样的这里就不注释了
    conv = Conv2D(num_channels=3, num_filters=1, filter_size=[3, 3], 
            param_attr=fluid.ParamAttr(
              initializer=NumpyArrayInitializer(value=w)))
    
    #. 将图片各种调整已符合卷积算子要求的样子
    # 将读入的图片转化为float32类型的numpy.ndarray
    x = np.array(img).astype('float32')
    # 图片读入成ndarry时，形状是[H, W, 3]，
    # 将通道这一维度调整到最前面
    x = np.transpose(x, (2,0,1))
    # 将数据形状调整为[N, C, H, W]格式
    x = x.reshape(1, 3, img.height, img.width)
    x = fluid.dygraph.to_variable(x)
    y = conv(x)
    out = y.numpy()

# 重复内容，不备注了
plt.figure(figsize=(20, 10))
f = plt.subplot(121)
f.set_title('input image', fontsize=15)
plt.imshow(img)
f = plt.subplot(122)
f.set_title('output feature map', fontsize=15)
plt.imshow(out.squeeze(), cmap='gray')
plt.show()
